home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (C) 1997-1998, Tall Tree Software Co.
- *
- * This code is provided AS-IS, with no warranty expressed or implied.
- * in no way shall Tall Tree Software Co. be held liable for damages
- * brought about by the use of this software.
- */
-
- #include <windows.h>
- #include <string.h>
- #include <stdio.h>
- #include <assert.h>
-
- #include <Rewriter Hook.h>
-
- char *sourceExtensions[] = { "c", "cc", "cpp", "cxx", "h", "hxx", "hpp", "inl",
- "idl",
- 0 };
-
- extern "C" DLLEXPORT bool RewriteSource( const char *sourceFile,
- const char *tmpFile )
- {
- // See if this is a C, C++, IDL, or VB file...
- const char *ext = strrchr( sourceFile, '.' );
- assert( ext != 0 );
- if ( ext == 0 )
- return( false );
- ext += 1;
- char **ep = sourceExtensions;
- while ( *ep != 0 && 0 != stricmp( ext, *ep ) )
- ++ep;
- if ( *ep == 0 )
- return( false ); // not a source code file.
-
-
- FILE *input = fopen( sourceFile, "r" );
- FILE *output = fopen( tmpFile, "w" );
- assert( input != 0 && output != 0 );
- if ( input == 0 || output == 0 ) {
- if ( input != 0 )
- fclose( input );
- if ( output != 0 )
- fclose( output );
- return( FALSE );
- }
-
- // Our plan here is to look for lines that end a /* */ comment or // that
- // are followed by blank lines.
- char thisLine[1024];
- char nextLine[1024];
- fgets( thisLine, sizeof(thisLine), input );
- for (;;) {
- if ( 0 != strstr( thisLine, "*/" ) &&
- strlen(strstr(thisLine,"*/")+2) ==
- strspn(strstr(thisLine,"*/")+2, " \t") ) {
- if ( 0 == fgets( nextLine, sizeof(nextLine), input ) ) {
- fputs( thisLine, output );
- break;
- }
- else if ( strlen( thisLine ) == strspn( thisLine, " \t\n" ) ) {
- *strstr(thisLine, "*/") = '\0';
- fputs( thisLine, output );
- /* Next line is blank, so put in an empty comment. */
- fputs( "*/\n", output );
- if ( 0 == fgets( thisLine, sizeof(thisLine), input ) )
- break;
- }
- else {
- fputs( thisLine, output );
- strcpy( thisLine, nextLine );
- }
- }
- else if ( 0 == strncmp( thisLine+strspn(thisLine, " \t"), "//", 2 ) ) {
- fputs( thisLine, output );
- if ( 0 == fgets( thisLine, sizeof(thisLine), input ) )
- break;
- else if ( strlen( thisLine ) == strspn( thisLine, " \t\n" ) ) {
- /* Next line is blank, so put in an empty comment. */
- fputs( "//\n", output );
- if ( 0 == fgets( thisLine, sizeof(thisLine), input ) )
- break;
- }
- }
- else {
- fputs( thisLine, output );
- if ( 0 == fgets( thisLine, sizeof(thisLine), input ) )
- break;
- }
- }
- fclose( output );
- fclose( input );
- return( TRUE );
- }
-
-